//
//  QpHotCountryViewController.m
//  Tour
//
//  Created by 才诗琦 on 16/3/9.
//  Copyright © 2016年 张中烨. All rights reserved.
//

#import "QpHotCountryViewController.h"
#import "PrefixHeader.pch"

@interface QpHotCountryViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UITableViewDataSource,UITableViewDelegate>




@end

@implementation QpHotCountryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back)];



    
    //数据处理
    [self dataHandler];
    
    /**<创建collectionView*/
    
    [self creatCollectionView];
    
    /**<创建tableView*/
    
    [self creatTableView];
    
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 创建tableView
- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -64, SCREEN_WIDTH, SCREEN_HEIGHT ) style:UITableViewStylePlain];
    

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.tableHeaderView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 590);
self.tableView.tableHeaderView = _collectionView;
//    [_tableView NightWithType:UIViewColorTypeNormal];
    [_tableView registerClass:[EatTableViewCell class] forCellReuseIdentifier:@"eat"];

    [self.view addSubview:_tableView];
    
    
    
}
#pragma mark - tableView的协议实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   
    
    return self.tableArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EatTableViewCell *eatCell = [tableView dequeueReusableCellWithIdentifier:@"eat" forIndexPath:indexPath];
   CollectionViewModel *eat = _tableArr[indexPath.row];
    NSLog(@"xzzxczx%@", _tableArr);
   
    [eatCell.photo sd_setImageWithURL:[NSURL URLWithString:eat.photo] placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
                //中文名字
    eatCell.cnname.text = eat.cnname;
                //EnglishiName
    eatCell.enname.text = eat.enname;
    
//    [eatCell.cnname NightWithType:UIViewColorTypeNormal];
//    [eatCell.enname NightWithType:UIViewColorTypeNormal];
//    [eatCell.photo NightWithType:UIViewColorTypeNormal];
    
//    [eatCell NightWithType:UIViewColorTypeNormal];
    
                return eatCell;
    //
    
    
}
#pragma mark - 设置tableView的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 110;
}

#pragma mark - tableView的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.hidesBottomBarWhenPushed=YES;
            QpFoodDetailsViewController *qp = [[QpFoodDetailsViewController alloc]init];
    
    
           CollectionViewModel *q = _tableArr[indexPath.row];
            qp.cityid = q.cityid ;
    
    qp.name = q.cnname;
   
    qp.photo = q.photo;
            NSLog(@"cityid: %@",qp.cityid);
            [self.navigationController pushViewController:qp animated:YES];
   

}
#pragma mark - 数据处理
- (void)dataHandler
{
    if (!([self.idStr isEqualToString:@"62"] | [self.idStr isEqualToString:@"10723"] | [self.idStr isEqualToString:@"10239"] | [self.idStr isEqualToString:@"7789"])) {
        // 通过传过来的热门国家id拼接网址
        NSString *url = [NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/country_detail?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&country_id=%@",self.idStr];
       
        NSString *str = @"&oauth_token=89dd34fbffaaa72503b4baa7a9d02812&page=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=CDA3EEEB-61BF-473E-AFFF-32B6332C9BBF&track_os=ios%25207.1.2&track_user_id=7328550&v=1";
        NSString *newStr = [NSString stringWithFormat:@"%@%@",url,str];
        
//        [self dataHandlerWithStr:newStr
//         ];
        [AFNModel GETWithHandler:newStr completion:^(id result) {
           
            /**< 合并数组 */  //拼接 热门城市
            self.tableArr = [CollectionViewModel modelHanderWithArray:result[@"data"][@"hot_mguide"]];
            
            self.dataArr = [CollectionViewModel modelHanderWithArray:result[@"data"][@"hot_city"]]; /**< 热门 城市*/
            
           
            NSString *str1 = result[@"data"][@"cnname"];
            NSString *str2 = result[@"data"][@"enname"];
            NSString *str3 = result[@"data"][@"entryCont"];
             NSMutableArray *photo = result[@"data"][@"photos"];
            
            UIView *newView =[[lunBoView alloc] initWithFrame:CGRectMake(0, 0, 100, 240) str:str1 str2:str2 str3:str3 arr:photo];
            
            [_collectionView addSubview:newView];
            // label 热门城市
            UILabel *labelcity = [[UILabel alloc]initWithFrame:CGRectMake(15, 210, SCREEN_WIDTH / 2, 20)];
            labelcity.text = [NSString stringWithFormat:@"%@城市",str1];
            labelcity.font = [UIFont systemFontOfSize:15];
            
//            [labelcity NightWithType:UIViewColorTypeNormal];
            [self.collectionView addSubview:labelcity];
            // 查看更多
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
            btn.frame = CGRectMake(WIDTH - 100 , 210, 100, 20);
            [btn setTitle:@"更多城市>" forState:UIControlStateNormal];
            
//            [btn NightWithType:UIViewColorTypeNormal];
            // 添加方法
            [btn addTarget:self action:@selector(getmore:) forControlEvents:UIControlEventTouchUpInside];
            [_collectionView addSubview:btn];
            // 刷新
            [self.tableView reloadData];
            [self.collectionView reloadData];

        }];
        

    

    }
 
    else if ([self.idStr isEqualToString:@" "]){
            
        }else{
        
            // 通过传过来的热门国家id拼接网址
            NSString *url = [NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/city_detail?city_id=%@",self.idStr];
            NSString *str = @"&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&oauth_token=89dd34fbffaaa72503b4baa7a9d02812&page=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=CDA3EEEB-61BF-473E-AFFF-32B6332C9BBF&track_os=ios%25207.1.2&track_user_id=7328550&v=1";
            NSString *newStr = [NSString stringWithFormat:@"%@%@",url,str];
            [AFNModel GETWithHandler:newStr completion:^(id result) {
                
                /**< 合并数组 */  //拼接 热门城市
                self.tableArr = [CollectionViewModel modelHanderWithArray:result[@"data"][@"hot_mguide"]];
                
                self.dataArr = [CollectionViewModel modelHanderWithArray:result[@"data"][@"hot_city"]]; /**< 热门 城市*/
                
                
                NSString *str1 = result[@"data"][@"cnname"];
                NSString *str2 = result[@"data"][@"enname"];
                NSString *str3 = result[@"data"][@"entryCont"];
                NSMutableArray *photo = result[@"data"][@"photos"];
                
                UIView *newView =[[lunBoView alloc] initWithFrame:CGRectMake(0, 0, 100, 240) str:str1 str2:str2 str3:str3 arr:photo];
                
                [_collectionView addSubview:newView];
                // label 热门城市
                UILabel *labelcity = [[UILabel alloc]initWithFrame:CGRectMake(15, 210, SCREEN_WIDTH / 2, 20)];
                labelcity.text = [NSString stringWithFormat:@"%@城市",str1];
                labelcity.font = [UIFont systemFontOfSize:15];
                
//                [labelcity NightWithType:UIViewColorTypeNormal];
                [self.collectionView addSubview:labelcity];
                // 查看更多
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
                btn.frame = CGRectMake(WIDTH - 100 , 210, 100, 20);
                [btn setTitle:@"更多城市>" forState:UIControlStateNormal];
                
//                [btn NightWithType:UIViewColorTypeNormal];
                // 添加方法
                [btn addTarget:self action:@selector(getmore:) forControlEvents:UIControlEventTouchUpInside];
                [_collectionView addSubview:btn];
                // 刷新
                [self.tableView reloadData];
                [self.collectionView reloadData];
                
            }];

    }
    // 查看更多
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(SCREEN_WIDTH - 100 , 210, 100, 20);
    [btn setTitle:@"查看全部>" forState:UIControlStateNormal];
//    [btn NightWithType:UIViewColorTypeNormal];
    
    // 添加方法
    [btn addTarget:self action:@selector(getmore:) forControlEvents:UIControlEventTouchUpInside];
    [_collectionView addSubview:btn];

    
}


#pragma mark - 创建CollectionView
- (void)creatCollectionView
{
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout alloc];
    //  设置item 大小
    layout.itemSize =CGSizeMake((SCREEN_WIDTH - 30 - 10) / 2, ((SCREEN_WIDTH - 30 - 10) / 2)* 226 /339 );
    //  设置 行列最小间距
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    // 滚动方向(竖向)
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    // 头部区域
    layout.headerReferenceSize = CGSizeMake(100, 240);// 垂直滚动 横坐标不生效
       
    // 创建 collectionview
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 590) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor  whiteColor];
    // 设置代理人
    _collectionView.delegate =self;
    _collectionView.dataSource = self;
    // 隐藏滚动条
    _collectionView.showsVerticalScrollIndicator = NO;

//   [_collectionView NightWithType:UIViewColorTypeNormal];
    // 自适应 边界
    _collectionView.bounces = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _collectionView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);
    
    
    
#warning  cell需要注册 才能使用
    [_collectionView registerClass:[QpHotCountryCollectionViewCell class] forCellWithReuseIdentifier:@"hot_citycell"];
    
}
#pragma mark - CollectionView两种协议方法
     - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
        return _dataArr.count ;
    }

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
     QpHotCountryCollectionViewCell *citycell = [collectionView dequeueReusableCellWithReuseIdentifier:@"hot_citycell" forIndexPath:indexPath];
        CollectionViewModel *q = _dataArr[indexPath.item];
        // 网络获取 图片
        [citycell.photo sd_setImageWithURL:[NSURL URLWithString:q.photo]placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
        // 中文名字
        citycell.cnname.text = q.cnname;
        // English Name
        citycell.enname.text = q.enname;
        
        if (_tableArr.count != 0) {
            UILabel *titleTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 470, 100, 40)];
            titleTextLabel.text = @"其他";
            
//            [titleTextLabel NightWithType:UIViewColorTypeNormal];
            [_collectionView addSubview:titleTextLabel];
            
        }
//        [citycell NightWithType:UIViewColorTypeNormal];
        return citycell;
        
    }

#pragma mark - 点击cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
  // 跳转隐藏 tabbar
    self.hidesBottomBarWhenPushed=YES;
    // 判断 前4个item 为城市
    if (indexPath.item < 4) {
        QpCityDetailsViewController *qp = [[QpCityDetailsViewController alloc]init];
        CollectionViewModel *q = _dataArr[indexPath.item];
        qp.cityid = q.cityid ;
        qp.str = q.cnname;
        [self.navigationController pushViewController:qp animated:YES];
        
            }
}

#pragma mark -  查看更多 btn 触发方法
- (void)getmore:(UIButton *)btn
{
    self.hidesBottomBarWhenPushed = YES;
    QpAllCityViewController *qp = [[QpAllCityViewController alloc]init];
    qp.countryid = self.idStr;
    [self.navigationController pushViewController:qp animated:YES];
    // 跳转隐藏 tabbar
    self.hidesBottomBarWhenPushed=NO;
 
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
